version: 2.1

executors:
  firefox:
    docker:
      - image: circleci/node:6
      - image: selenium/standalone-firefox:2.48.2
  chrome:
    docker:
      - image: circleci/node:6
      - image: selenium/standalone-chrome:2.48.2
        environment:
          # workaround for https://github.com/SeleniumHQ/docker-selenium/issues/87
          DBUS_SESSION_BUS_ADDRESS: /dev/null

commands:
  checkout_repo:
    steps:
      - checkout
      - run:
          name: Checkout submodule
          command: |
              git submodule sync
              git submodule update --init --recursive
      - run:
          name: Get changed files
          command: |
              if [[ $CIRCLE_PULL_REQUEST ]]; then
                BASE_COMMIT=$(curl -s -H "Authorization: token a61ecb2fc5b72da54431""1b3db3875c96854958a8" \
                  https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls/$CIRCLE_PR_NUMBER \
                  | jq -r ".base.sha | select(. != null)")...
              else
                BASE_COMMIT=HEAD^
              fi
              if [[ $BASE_COMMIT ]]; then
                CHANGED=$(git diff --name-only $BASE_COMMIT)
                echo "$CHANGED"
                echo "export CHANGED=\"$CHANGED\"" >> $BASH_ENV
              fi

  skip_if_only_changed:
    parameters:
      filter:
        type: string
    steps:
      - run:
          name: Skip tests if only "<< parameters.filter >>" are changed
          command: echo "$CHANGED" | grep -qvE '<< parameters.filter >>' || circleci step halt

  install_dependencies:
    steps:
      - restore_cache:
          keys:
            - yarn-deps-v3-{{ checksum "yarn.lock" }}
            - yarn-deps-v3-
      - run:
          name: Install dependencies
          # upgrade Yarn before installing dependencies, if needed
          command: |
              if ! yarn check; then
                curl -o- -L https://yarnpkg.com/install.sh | PROFILE=$BASH_ENV bash
                source $BASH_ENV
                yarn
              fi
      - save_cache:
          key: yarn-deps-v3-{{ checksum "yarn.lock" }}
          paths:
            - node_modules

  codecov:
    parameters:
      flag:
        type: string
    steps:
      - run:
          name: Upload code coverage reports to Codecov
          # do not upload if screenshotter tests are skipped
          command: ./node_modules/.bin/codecov -F "<< parameters.flag >>"

jobs:
  test:
    docker:
      - image: circleci/node:6
    steps:
      - run:
          name: Check whether the build is running on the main repository
          command: |
              if [[ $CIRCLE_PULL_REQUEST && $CIRCLE_PROJECT_USERNAME != "KaTeX" ]]; then
                echo "Please disable CircleCI on your forked repository!"
                exit 1
              fi

      - checkout_repo
      - skip_if_only_changed:
          filter: '^docs/|^LICENSE|\.md$'
      - install_dependencies

      - run:
          name: Lint code
          command: yarn test:lint
      - skip_if_only_changed:
          filter: '^static/|^website/'
      - run:
          name: Run Flow and Jest tests
          command: |
              yarn test:flow
              yarn test:jest --coverage
      - codecov:
          flag: test

  screenshotter:
    parameters:
      browser:
        type: executor
    executor: << parameters.browser >>
    steps:
      - checkout_repo
      - skip_if_only_changed:
          filter: '^docs/|^static/|^website/|^LICENSE|\.md$'
      - install_dependencies

      - run:
          name: Verify screenshots and generate diffs and new screenshots
          command: node dockers/screenshotter/screenshotter.js --selenium-ip localhost -b $CIRCLE_JOB --verify --diff --new --coverage
      - codecov:
          flag: screenshotter
      - store_artifacts:
          path: test/screenshotter/new
          destination: new
      - store_artifacts:
          path: test/screenshotter/diff
          destination: diff

workflows:
  test:
    jobs:
      - test
      - screenshotter:
          name: firefox
          browser: firefox
      - screenshotter:
          name: chrome
          browser: chrome
